local hx__jisuan2 = fk.CreateSkill {
  name = "hx__jisuan2",
  tags = { Skill.Compulsory },
}
hx__jisuan2:addEffect(fk.CardUsing, {
  mute = true,
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__jisuan2.name) and data.card.number > 0 and
    data.extra_data and data.extra_data.hx__jisuan2 and (data.extra_data.hx__jisuan2 % data.card.number == 0
    or data.card.number % data.extra_data.hx__jisuan2 == 0)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(hx__jisuan2.name)
    player.room:doAnimate("InvokeSkill", {
      name = "hx__jisuan2",
      player = player.id,
      skill_type = "special",
    })
    if player:getMark("@hx__jisuan2_count-turn") < 3 then
      room:addPlayerMark(player, "@hx__jisuan2_count-turn", 1)
    end
    local x = player:getMark("@hx__jisuan2_count-turn")
    if room:askToSkillInvoke(player, { skill_name = hx__jisuan2.name, prompt = "点确定令你使用的下一张牌不计次数限制且结算次数+1或伤害+1，取消则摸"..x.."张牌" }) then
      room:addPlayerMark(player, "@@hx__jisuan2", 1)
    else
      player:drawCards( x , hx__jisuan2.name)
    end
    if x == 3 then
      room:setPlayerMark(player, "@hx__jisuan2_count-turn", 0)
    end
  end,
})

hx__jisuan2:addEffect(fk.CardResponding, {
  mute = true,
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__jisuan2.name) and data.card.number > 0 and data.card and
    data.extra_data and data.extra_data.hx__jisuan2 and (data.extra_data.hx__jisuan2 % data.card.number == 0
    or data.card.number % data.extra_data.hx__jisuan2 == 0)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(hx__jisuan2.name)
    player.room:doAnimate("InvokeSkill", {
      name = "hx__jisuan2",
      player = player.id,
      skill_type = "special",
    })
    if player:getMark("@hx__jisuan2_count-turn") < 3 then
      room:addPlayerMark(player, "@hx__jisuan2_count-turn", 1)
    end
    local x = player:getMark("@hx__jisuan2_count-turn")
    if room:askToSkillInvoke(player, { skill_name = hx__jisuan2.name, prompt = "点确定令你使用的下一张牌不计次数限制且结算次数+1或伤害+1，取消则摸"..x.."张牌" }) then
      room:addPlayerMark(player, "@@hx__jisuan2", 1)
    else
      player:drawCards( x , hx__jisuan2.name)
    end
    if x == 3 then
      room:setPlayerMark(player, "@hx__jisuan2_count-turn", 0)
    end
  end,
})

hx__jisuan2:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__jisuan2.name, true)
  end,
  on_refresh = function(self, event, target, player, data)
    local lastNumber = player:getMark("@hx__jisuan2_number")
    local realNumber = math.max(data.card.number, 0)
    player.room:setPlayerMark(player, "@hx__jisuan2_number", realNumber)
    if lastNumber > 0 then
      data.extra_data = data.extra_data or {}
      data.extra_data.hx__jisuan2 = lastNumber
    end
  end,
})

hx__jisuan2:addEffect(fk.CardResponding, {
  priority = 2,
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__jisuan2.name, true)
  end,
  on_refresh = function(self, event, target, player, data)
    local lastNumber = player:getMark("@hx__jisuan2_number")
    local realNumber = math.max(data.card.number, 0)
    player.room:setPlayerMark(player, "@hx__jisuan2_number", realNumber)
    if lastNumber > 0 then
      data.extra_data = data.extra_data or {}
      data.extra_data.hx__jisuan2 = lastNumber
    end
  end,
})

hx__jisuan2:addAcquireEffect(function (self, player, is_start)
  if not is_start then
    local room = player.room
    room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
      local use = e.data
      if use.from == player then
        room:setPlayerMark(player, "@hx__jisuan2_number", use.card.number)
        return true
      end
    end, 0)
  end
end)
hx__jisuan2:addLoseEffect(function (self, player)
  player.room:setPlayerMark(player, "@hx__jisuan2_number", 0)
end)

hx__jisuan2:addEffect("targetmod", {
  name = "hx__jisuan2",
  bypass_times = function(self, player, skill, scope, card)
    return card and player:getMark("@@hx__jisuan2") > 0
  end,
})

hx__jisuan2:addEffect(fk.PreCardUse, {
  mute = true,
  is_delay_effect = true,
  anim_type = "offensive",
  can_refresh = function(self, event, target, player, data)
    if target == player and player:hasSkill(hx__jisuan2.name) then
      return player:getMark("@@hx__jisuan2") > 0
    end
  end,
  on_refresh = function(self, event, target, player, data)
    player:broadcastSkillInvoke(hx__jisuan2.name)
    player.room:doAnimate("InvokeSkill", {
      name = "hx__jisuan2",
      player = player.id,
      skill_type = "special",
    })
    if player.room:askToSkillInvoke(player, { skill_name = hx__jisuan2.name, prompt = "点确定令此牌结算次数+1，取消则此牌伤害+1" }) then
      data.additionalEffect = (data.additionalEffect or 0) + 1
    else
      data.additionalDamage = (data.additionalDamage or 0) + 1
    end
    player.room:setPlayerMark(player, "@@hx__jisuan2", 0)
  end,
})



return hx__jisuan2